﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Resources;
using System.Windows.Data;


namespace MusicAlarm7.DataModel
{
    public class AlarmViewModel : INotifyPropertyChanged, IDisposable
    {
        private AlarmDataContext alarmDB;
        private string connectionString;

        public AlarmViewModel()
        {
            this.connectionString = "Data Source=isostore:/alarm.sdf";
            alarmDB = new AlarmDataContext(connectionString);

#if DEBUG
            if (alarmDB.DatabaseExists()) alarmDB.DeleteDatabase();
#endif
            if (alarmDB.DatabaseExists() == false)
            {
                try
                {
                    alarmDB.CreateDatabase();
                    //初始化铃声数据
                    Music m1 = new Music() { Id = 1, FileName = "ListeningEarcon.wav" };
                    alarmDB.Musics.InsertOnSubmit(m1);
                    Music m2 = new Music() { Id = 2, FileName = "DoneListeningEarcon.wav" };
                    alarmDB.Musics.InsertOnSubmit(m2);
                    alarmDB.SubmitChanges();
                }
                catch (Exception)
                {
                    if (alarmDB.DatabaseExists() == true) alarmDB.DeleteDatabase();
                    throw;
                }
            }
        }

        public void Commit()
        {
            alarmDB.SubmitChanges();
        }

        public void Rollback()
        {
            this.alarmDB = null;
            this.alarmDB = new AlarmDataContext(this.connectionString);
        }

        private ObservableCollection<AlarmItem> _alarms;
        public ObservableCollection<AlarmItem> Alarms
        {
            get { return _alarms; }
            set
            {
                _alarms = value;
                NotifyPropertyChanged("Alarms");
            }
        }

        private AlarmItem _currentAlarm;
        public AlarmItem CurrentAlarm
        {
            get { return _currentAlarm; }
            set
            {
                _currentAlarm = value;
                NotifyPropertyChanged("CurrentAlarm");
            }
        }

        private ObservableCollection<Music> _musics = null;
        public ObservableCollection<Music> Musics
        {
            get { return _musics; }
            set
            {
                _musics = value;
                NotifyPropertyChanged("Musics");
            }
        }

        public void LoadFromDatabase()
        {
            var alarmInDB = from alarm in alarmDB.Items
                            select alarm;
            Alarms = new ObservableCollection<AlarmItem>(alarmInDB);
            var musicInDB = from music in alarmDB.Musics
                            select music;
            Musics = new ObservableCollection<Music>(musicInDB);
        }

        public void AddAlarm(AlarmItem newAlarm)
        {
            alarmDB.Notifications.InsertAllOnSubmit(newAlarm.Notifications);

            alarmDB.Items.InsertOnSubmit(newAlarm);

            newAlarm.SetAlarm();
            alarmDB.SubmitChanges();
            Alarms.Add(newAlarm);
        }


        public void DeleteAlarm(AlarmItem alarmToDel)
        {
            alarmToDel.UnsetAlarm();
            Alarms.Remove(alarmToDel);

            //var notis = from noti in alarmDB.Notifications
            //            where noti._alarmId == alarmToDel.AlarmId
            //            select noti;
            //alarmDB.Notifications.DeleteAllOnSubmit(notis);
            alarmDB.Items.DeleteOnSubmit(alarmToDel);

            alarmDB.SubmitChanges();
        }


        public void ModifyAlarm(AlarmItem alarmItem)
        {
            var notis = from noti in alarmDB.Notifications
                        where noti._alarmId == alarmItem.AlarmId
                        select noti;
            alarmDB.Notifications.DeleteAllOnSubmit(notis);
            alarmDB.Notifications.InsertAllOnSubmit(alarmItem.Notifications);
            alarmItem.SetAlarm();
            alarmDB.SubmitChanges();
        }


        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        protected virtual void Dispose(bool flag)
        {
            if (flag)
            {
                //清理托管资源
                if (this.alarmDB != null)
                    this.alarmDB.Dispose();
            }
            //清理本机资源
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}